﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyShowBinTree
{
    public partial class Form2 : Form
    {
        private TreeNode root = null;

        public Form2()
        {
            InitializeComponent();
        }

        public void showArr(int[] arr) {
            string strArr = string.Join(",",arr);
            strArr = "[" + strArr + "]";
            txtData.AppendText(strArr+Environment.NewLine);
        }

        public int[] GetArr() {
            int[] arr = new int[] { -3, -3, -1, -3, 1, -2, 0 };
            return arr;
        }

        private void 最小堆ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int[] arr = GetArr();
            showArr(arr);
            MinHeap min = new MinHeap(arr);
            showArr(min.Arr);
            Image img = min.getImage();
            MyPic.Image=img;
        }

        private void 最大堆ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 原生堆ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int[] arr = GetArr();
            showArr(arr);
            TreeNode root=this.GetTreeNode(0, arr);
            Image img = Common.GetTreeImage(root);
            MyPic.Image = img;
        }

        private TreeNode GetTreeNode(int index, int[] arr)
        {
            if (index >= arr.Length) {
                return null;
            }
            TreeNode node = new TreeNode(arr[index]);
            node.left = GetTreeNode(index * 2 + 1, arr);
            node.right = GetTreeNode(index * 2 + 2, arr);
            return node;
        }

        private void 层次遍历ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int[] arr = GetArr();
            showArr(arr);
            TreeNode root = this.GetTreeNode(0, arr);
            Image img = Common.GetTreeImage(root);
            MyPic.Image = img;
            Dictionary<int, List<int>> dic = new Dictionary<int, List<int>>();
            EachTreeNode(root, dic, 0);
            List<List<int>> list = new List<List<int>>();
            foreach (var item in dic)
            {
                if (item.Key % 2 != 0)
                {
                    item.Value.Reverse();
                   
                }
                list.Add(item.Value);
            }

            foreach (var item in list)
            {
                showArr(item.ToArray());
            }
        }

        private void EachTreeNode(TreeNode node, Dictionary<int, List<int>> dic,int depth)
        {
            if (node == null) {
                return;
            }
          
            if (!dic.ContainsKey(depth))
            {
               dic[depth]=new List<int>();
            }
            dic[depth].Add(node.value);
            EachTreeNode(node.left, dic, depth + 1);
            EachTreeNode(node.right, dic, depth + 1);
        }
    }
}
